package xyz.liut.liutlibraries.baseclass.globalcatcher;

import java.util.HashMap;
import java.util.Map;

import xyz.liut.volley.LiutVolley;
import xyz.liut.volley.VolleyListener;

/**
 * @author Liut
 * @ProjectName GlobalCatcher
 * @Description 网络
 * @date 2016/7/11
 */
public class AppApi {

    private static final String TAG = "AppApi";
    private static LiutVolley volley;
    public static final String URL = "http://120.26.222.16:1111";
    public static String IMG_URL = URL + "/api/GetImage?path=";

    public AppApi(LiutVolley volley) {
        AppApi.volley = volley;
    }


    //微信获取OPNE_ID
    public static void weiXOpenId(String code, VolleyListener listener) {
        volley.getStringRequest("https://api.weixin.qq.com/sns/oauth2/access_token?" +
                        "appid=" + AppConstants.WX_APP_ID +
                        "&secret=" + AppConstants.WX_APP_SECRET +
                        "&code=" + code +
                        "&grant_type=authorization_code",
                listener);
    }


    //微信获取用户信息
    public static void weiXGetUserInfo(String accessToken, String openID, VolleyListener lsitener) {
        volley.getStringRequest("https://api.weixin.qq.com/sns/userinfo?" +
                "access_token=" + accessToken + "&" +
                "openid=" + openID, lsitener);
    }


    //微信登录
    public static void loginByWeChat(String name, String path, String openid, int sex, VolleyListener listener) {
        if (sex == 1) {
            sex = 2;
        } else {
            sex = 1;
        }
        Map<String, String> params = new HashMap<>();
        params.put("name", name);
        params.put("path", path);
        params.put("openid", openid);
        params.put("sex", sex + "");
        volley.postStringRequest(URL + "/api/AppLogin", params, listener);
    }

    //商品分类
    public static void goodsList(VolleyListener listener) {
        volley.getStringRequest(URL + "/api/QueryGoodsClassify", listener);
    }

    /**
     * 所有商品
     *
     * @param pageNum   页码；
     * @param salesSort 是否销量排序,0:不要，1：要
     * @param priceSort 是否price排序,0:不要，1：要
     */
    public static void allGoods(int pageNum, int salesSort, int priceSort, VolleyListener listener) {
        volley.getStringRequest(URL + "/api/QueryGoods?" +
                "pageNum=" + pageNum + "&" +
                "pageSize=10&" +
                "salesSort=" + salesSort + "&" +
                "priceSort=" + priceSort, listener);
    }

    //加入购物车
    public static void putInCart(String goodsID, int number, String token, VolleyListener listener) {
        volley.getStringRequest(URL + "/api/AddToShoppingCart" +
                "?goodsid=" + goodsID +
                "&number=" + number +
                "&token=" + token, listener);
    }

    //修改购物车数量
    public static void updateCartNum(String shopcartid, int buynumber, String token, VolleyListener listener) {
        volley.getStringRequest(URL + "/api/UpDateShoppingCart" +
                "?shopcartid=" + shopcartid +
                "&buynumber=" + buynumber +
                "&token=" + token, listener);
    }

    //查看我的购物车
    public static void cart(String token, int pageNum, VolleyListener listener) {
        volley.getStringRequest(URL + "/api/QueryMyShoppingCart" +
                        "?token=" + token +
                        "&pageNum=" + pageNum +
                        "&pageSize=10",
                listener);
    }

    //删除购物车
    public static void delCart(String shopCardID, String token, VolleyListener listener) {
        volley.getStringRequest(URL + "/api/DelShoppingCart" +
                "?shopcartid=" + shopCardID +
                "&token=" + token +
                "&api_key=cart", listener);
    }


    //查询商品by classifyID
    public static void queryGoodsByClassifys(int pageNum, int pageSize, int classifyid, VolleyListener listener) {
        volley.getStringRequest(URL + "/api/QueryGoodsByClassifys" +
                        "?pageNum=" + pageNum +
                        "&pageSize=" + pageSize +
                        "&classifyid=" + classifyid,
                listener);
    }

    //获取购物车总数
    public static void queryCartCount(String token, VolleyListener listener) {
        volley.getStringRequest(URL + "/api/QueryShoppingCartCount?token=" + token, listener);
    }


    //查询地区
    public static void queryRegion(int parentid, VolleyListener listener) {
        volley.getStringRequest(URL + "/api/QueryRegion?parentid=" + parentid, listener);
    }

    //添加地址
    public static void addAddress(String regionpath,              //区域编码,如:101/101002/101002001/
                                  String regionname,                //区域名称,如:浙江省/台州市/黄岩区/
                                  String address,                   //详细地址
                                  int isdefault,                    //是否设为默认地址，1是默认，0不是
                                  String name,                      //联系人名字
                                  String phone,
                                  String token,
                                  VolleyListener listener) {
        volley.getStringRequest(URL + "/api/AddDeliveryAddress" +
                        "?regionpath=" + regionpath +
                        "&regionname=" + regionname +
                        "&address=" + address +
                        "&isdefault=" + isdefault +
                        "&name=" + name +
                        "&phone=" + phone +
                        "&token=" + token,
                listener);
    }

    //修改地址
    public static void updateAddress(int addressID, String regionpath, String regionname, String address, int isdefault, String name, String phone, String token, VolleyListener listener) {
        volley.getStringRequest(URL + "/api/UpdateMyAdress?" +
                        "adressid=" + addressID +
                        "&regionpath=" + regionpath +
                        "&regionname=" + regionname +
                        "&address=" + address +
                        "&name=" + name +
                        "&phone=" + phone +
                        "&token=" + token,
                listener);
    }

    //查询地址
    public static void queryAddrs(String token, VolleyListener listener) {
        volley.getStringRequest(URL + "/api/QueryMyDeliveryAddress?pageNum=1&pageSize=1123&token=" + token, listener);
    }

    //获取默认地址
    public static void getDefaultAddr(String token, VolleyListener listener) {
        volley.getStringRequest(URL + "/api/QueryMyDefaultAddress?token=" + token, listener);
    }

    //删除地址
    public static void delAddress(int cm_deliveryaddressid, String token, VolleyListener listener) {
        volley.getStringRequest(URL + "/api/DelMyAdress?token=" + token +
                "&adressid=" + cm_deliveryaddressid, listener);
    }

    //设置默认地址
    public static void setDefaultAddress(String token, int id, VolleyListener listener) {
        volley.getStringRequest(URL + "/api/SetMyDefaultAdress" +
                        "?token=" + token +
                        "&adressid=" + id,
                listener);
    }

    //获取商品详情
    public static void goodsDetail(String goodsid, VolleyListener listener) {
        volley.getStringRequest(URL + "/api/QueryGoodsInfo?goodsid=" + goodsid, listener);
    }

    //获取热搜词
    public static void QueryHotSearchTerms(VolleyListener listener) {
        volley.getStringRequest(URL + "/api/QueryHotSearchTerms", listener);
    }


    //获得物流信息
    public static void QueryWulData(VolleyListener listener) {
        volley.getStringRequest(URL + "/api/QueryLogistics", listener);
    }

    //创建订单
    public static void createOrder(String token, String cartids, int adressid, int logisticsid, VolleyListener listener) {
        // 提交订单 (参数=>
        // token
        // cartids:购物车ID.多个ID请用|拼接.如88|99|101|.注意最后一个符号必须是|,
        // adressid:地址ID,
        // logisticsid:物流ID)
        // (返回值=>101：秘钥错误，200：数据库异常，1：参数未填写完整，2：购物车ID错误，3：商品已下架或删除，4：商品库存不足)
        Map<String, String> params = new HashMap<>();
        params.put("cartids", cartids);
        params.put("adressid", adressid + "");
        params.put("logisticsid", logisticsid + "");
        params.put("token", token);
        volley.postStringRequest(URL + "/api/CreateOrder", params, listener);
    }

    //查询订单 /api/QueryMyOrders?token=123&type=5&pageNum=1&pageSize=11
    public static void queryOrders(String token, int type, int pageNum, VolleyListener listener) {
        volley.getStringRequest(URL + "/api/QueryMyOrders" +
                        "?token=" + token +
                        "&type=" + type +
                        "&pageNum=" + pageNum +
                        "&pageSize=10",
                listener);
    }


    //搜索商品
    public static void searchGoods(int pageNum, String content, VolleyListener listener) {
        volley.getStringRequest(URL + "/api/SearchGoods?pageNum=" + pageNum +
                "&pageSize=10&content=" + content, listener);
    }
}








